package com.lvmama.test;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.<p/>
 * User: troy-kou<p/>
 * Date: 16/1/27<p/>
 * Time: 下午1:44<p/>
 * Email:kouhongyu@163.com<p/>
 */
public class GroovyTest3 {

    public static void main(String[] args) throws IOException, ScriptException, NoSuchMethodException, InterruptedException {

        long time = System.currentTimeMillis();
        long now;

        /*String path1 = "/Users/troy/.m2/repository/com/lvmama/comm/lvmama_task/1.0-SNAPSHOT/lvmama_task-1.0-SNAPSHOT.jar";
        String path2 = "/Users/troy/work/IdeaProjects/Lvmama/LvTrunk/focus/pet/pet_public/target/pet_public/WEB-INF/lib/lvmama_comm-1.0.0-SNAPSHOT.jar";*/
        
        String path1 = "D:\\Tools\\.m2\\repository\\com\\lvmama\\comm\\lvmama_task\\1.0-SNAPSHOT\\lvmama_task-1.0-SNAPSHOT.jar";
        String path2 = "D:\\Tools\\.m2\\repository\\com\\lvmama\\comm\\lvmama_comm\\1.0.0-SNAPSHOT\\lvmama_comm-1.0.0-SNAPSHOT.jar";
        //String path3 = "D:\\Tools\\.m2\\repository\\com\\lvmama\\comm\\security-interface\\1.0.0-SNAPSHOT\\security-interface-1.0.0-SNAPSHOT.jar";

        File file1 = new File(path1);
        File file2 = new File(path2);
        //File file3 = new File(path3);

        List<URL> urls = new ArrayList<URL>();

        urls.add(file1.toURI().toURL());
        urls.add(file2.toURI().toURL());
        //urls.add(file3.toURI().toURL());

        URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class<? extends URLClassLoader> sysclass = URLClassLoader.class;
        try {
            Method method = sysclass.getDeclaredMethod("addURL", URL.class);
            method.setAccessible(true);
            for (URL url : urls) {
                method.invoke(sysloader, url);
            }
        } catch (Throwable t) {
            throw new IOException("Error, could not add URL to system classloader", t);
        }

        now = System.currentTimeMillis();
        System.out.println("############################## load jar time long = " + (now - time));
        time = now;

        ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");


        
        String script3 = "" +
                "import org.springframework.context.support.ClassPathXmlApplicationContext;" +
                "import com.lvmama.prism.security.dao.LvSecretkeyInterface;" +
                "import com.lvmama.pet.sync.service.util.ContextFactory;" +

                "void execute(){" +

//                "        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(\"dubbo-consumer.xml\");\n" +
//                "        context.start();\n" +
                "        LvSecretkeyInterface lvSecretkeyInterface = (LvSecretkeyInterface) ContextFactory.getInstance().context.getBean(\"lvSecretKeyService\");\n" +
                
                "Map<String, Object> map2 = new HashMap<String,Object>();"+
        		"map2.put(\"groupId\", \"LvKey_PHP_CRM\");"+
        		"map2.put(\"content\", \"{id:1}\");"+
        		"String signature = null;"+
        		"try {"+
        			"signature = lvSecretkeyInterface.generateSign(map2);"+
            		"System.out.println(signature);"+
			     "} catch (Exception e) {"+
					// TODO Auto-generated catch block
					"e.printStackTrace();"+
				"}"+
               

                "}"; 
        
       /* List list = new ArrayList();
        list.add("dubbo-consumer.xml");
        list.add("spring-config-dubbo-prism-service-consumer.xml");
        
        String[] configs = (String[]) list.toArray();
        
        ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext(configs);*/
        

       /* ContextFactory.getInstance().putContext("dubbo-consumer.xml");

        ContextFactory.getInstance().putContext("spring-config-dubbo-prism-service-consumer.xml");*/


//        ContextFactory.getInstance().getContext("dubbo-consumer.xml");
        now = System.currentTimeMillis();
        System.out.println("############################## init context time long = " + (now - time));
        time = now;





      
        
       engine.eval(script3);
        ((Invocable) engine).invokeFunction("execute");
        now = System.currentTimeMillis();
        System.out.println("############################## script time long = " + (now - time));
        time = now;




    }
}
